package i.i.myapplication;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

    private Fragment NewsFragment;
    private Fragment MusicFragment;
    private Fragment VideoFragment;
    private Fragment WeChatListFragment;
    private Fragment MineFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);  // 确保有一个 BottomNavigationView 和 FrameLayout

        // 初始化Fragment
        NewsFragment = new NewsFragment();
        MusicFragment = new MusicFragment();
        VideoFragment = new VideoFragment();
        WeChatListFragment  = new WeChatFragment();
        MineFragment = new MineFragment();

        // 默认加载SettingsFragment-1
//        loadFragment(MineFragment);
        loadFragment(MusicFragment);

        // 设置BottomNavigationView监听
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(item -> {
            int itemId = item.getItemId();
            if (itemId == R.id.menu_home) {
                loadFragment(NewsFragment);
                return true;
            } else if (itemId == R.id.menu_insights) {
                loadFragment(MusicFragment);
                return true;
            } else if (itemId == R.id.menu_nutri_coach) {
                loadFragment(VideoFragment);
                return true;
            } else if (itemId == R.id.menu_wechat) {
                loadFragment(WeChatListFragment);
                return true;
            }
            else if (itemId == R.id.menu_settings) {
                loadFragment(MineFragment);
                return true;
            }
            return false;
        });


        // 设置默认选中项为Settings-2
        bottomNavigationView.setSelectedItemId(R.id.menu_insights);
    }

    private void loadFragment(Fragment fragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, fragment);
        transaction.commit();
    }
}
